package com.boot.utils.excel;

import com.alibaba.excel.EasyExcel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: HuangX
 * @create: 2022/5/14
 */
@Slf4j
public class ExcelDataReader<T> {

    private final List<List<String>> list = new ArrayList<>();

    private final List<ErrorMessage> errorMessageList = new ArrayList<>();

    /**
     * 上传的文件内容
     */
    private MultipartFile multipartFile;

    /**
     * 读取Excel文件最大行数
     */
//    private final long limitRows;

//    private Long lineNum = 1L;

    public List<T> readExcel(HttpServletRequest request, Class<T> clazz) throws IOException {
        ServletInputStream inputStream = request.getInputStream();

        DefaultAnalysisEventListener<T> listener = BeanUtils.instantiateClass(DefaultAnalysisEventListener.class);

        EasyExcel.read(inputStream, clazz, listener).sheet().doRead();

        return listener.getList();
    }


}
